package com.loongtech.bi.manager.game;

import com.loongtech.bi.entity.game.EntityActiveUserSchool;
import com.loongtech.bi.entity.game.EntityNewUserSchool;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.chart.*;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Map.Entry;

/**
 * 职业分析-manager
 * 
 */
@Service
public class SchoolGameManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	public void updateSchoolData(Date dateStart, Date dateEnd,
			List<EntityActiveUserSchool> activeUserSchoolList,
			List<EntityNewUserSchool> newUserSchoolList,
			List<SchoolGameSumData> schoolGameSumData,
			List<ChartUIData> pieChartList, List<SchoolGameData> schoolGameData) {
		
		Map<Integer, SchoolGameChartData> tmpSchoolGameChartData = new LinkedHashMap<>();  // 职业ID=图表类
		for (Entry<String, String> entry : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).entrySet()) {
			if (!GameParamConfigManager.ALL_FLAT.equals(entry.getKey())) {
				tmpSchoolGameChartData.put(Integer.parseInt(entry.getKey()), new SchoolGameChartData(Integer.parseInt(entry.getKey())));
			}
		}
		Map<String, SchoolGameData> tmpSchoolGameData = new LinkedHashMap<>();
		
		if (activeUserSchoolList != null && activeUserSchoolList.size() > 0) {
			// 总体
			SchoolGameSumData tmp = new SchoolGameSumData();
			tmp.setTitle("活跃用户");
			int sum = 0;
			
			for (EntityActiveUserSchool e : activeUserSchoolList) {
				int oid = e.getOccupationid();
				int num = e.getUser_number();
				String startDateStr = e.getStartday();
				String endDateStr = e.getEndday();
				
				if (Time.getDateStr(dateStart).equals(startDateStr) && Time.getDateStr(dateEnd).equals(endDateStr)) {
					// 总体
					tmp.getSchoolCounts().put(oid + "", num);
					sum += num;
					
					// 图表
					SchoolGameChartData t = tmpSchoolGameChartData.get(oid);
					if (t == null) {
						t = new SchoolGameChartData(oid);
						tmpSchoolGameChartData.put(oid, t);
					}
					t.setActiveusercount(num);
				}
				
				// 详细
				if (startDateStr.equals(endDateStr)) {  // 某天
					SchoolGameData t = tmpSchoolGameData.get(startDateStr);
					if (t == null) {
						t = new SchoolGameData();
						t.setDateStr(startDateStr);
						tmpSchoolGameData.put(startDateStr, t);
					}
					t.getActiveUserSchoolCounts().put(oid + "", num);
					t.setActiveUser(t.getActiveUser() + num);
				}
			}
			
			// 总体
			tmp.setSum(sum);
			schoolGameSumData.add(tmp);
		}
		
		if (newUserSchoolList != null && newUserSchoolList.size() > 0) {
			// 总体
			SchoolGameSumData tmp = new SchoolGameSumData();
			tmp.setTitle("新增用户");
			int sum = 0;
			
			for (EntityNewUserSchool e : newUserSchoolList) {
				int oid = e.getOccupationid();
				int num = e.getUser_number();
				String dateStr = Time.getDateStr(e.getDay());
				
				// 总体
				if (tmp.getSchoolCounts().get(oid + "") == null) {
					tmp.getSchoolCounts().put(oid + "", num);
				} else {
					tmp.getSchoolCounts().put(oid + "", tmp.getSchoolCounts().get(oid + "") + num);
				}
				sum += num;
				
				// 图表
				SchoolGameChartData t = tmpSchoolGameChartData.get(oid);
				if (t == null) {
					t = new SchoolGameChartData(oid);
					tmpSchoolGameChartData.put(oid, t);
				}
				t.setNewusercount(t.getNewusercount() + num);
				
				// 详细
				SchoolGameData t1 = tmpSchoolGameData.get(dateStr);
				if (t1 == null) {
					t1 = new SchoolGameData();
					t1.setDateStr(dateStr);
					tmpSchoolGameData.put(dateStr, t1);
				}
				t1.getNewUserSchoolCounts().put(oid + "", num);
				t1.setNewUser(t1.getNewUser() + num);
			}
			
			// 总体
			tmp.setSum(sum);
			schoolGameSumData.add(tmp);
		}
		
		// 图表
		if (tmpSchoolGameChartData.size() > 0) {
			List<SchoolGameChartData> t = new ArrayList<SchoolGameChartData>(tmpSchoolGameChartData.values());
			Collections.sort(t, new SchoolGameChartData().comparatorId);
			
			for (int i = 1; i <= 2; i++) {
				ChartUIData ct = new ChartUIData();
				ct.setJsId(i + "");
				ct.setJsonStr(new ChartTool(t, SchoolGameChartData.class, i == 1 ? "activeusercount" : "newusercount").loadData("pie"));
				ct.setJsonType(ChartType.PIE.type);
				ct.setHeader(i == 1 ? "活跃用户职业分布" : "新增用户职业分布");
				pieChartList.add(ct);
			}
		}
		
		// 详细
		if (tmpSchoolGameData.size() > 0) {
			schoolGameData.addAll(new ArrayList<>(tmpSchoolGameData.values()));
			Collections.sort(schoolGameData, new SchoolGameData().comparatorDate);
		}
	}
	
	// 总体
	public class SchoolGameSumData {
		private String title;
		private int sum;  // 总数
		private Map<String, Integer> schoolCounts;  // 职业ID=人数

		public SchoolGameSumData() {
			schoolCounts = new LinkedHashMap<>();
			for (Entry<String, String> entry : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).entrySet()) {
				if (!GameParamConfigManager.ALL_FLAT.equals(entry.getKey())) {
					schoolCounts.put(entry.getKey(), 0);
				}
			}
		}
		
		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public int getSum() {
			return sum;
		}

		public void setSum(int sum) {
			this.sum = sum;
		}

		public Map<String, Integer> getSchoolCounts() {
			return schoolCounts;
		}

		public void setSchoolCounts(Map<String, Integer> schoolCounts) {
			this.schoolCounts = schoolCounts;
		}
	}
	
	// 总体图表
	public class SchoolGameChartData {
		private int schoolid;
		@ChartFlow(name = "职业")
		private String school;
		@ChartSelect(name = "活跃用户职业分布", key = 1)
		@ChartData1(name = "活跃用户职业分布", key = 1)
		private int activeusercount;
		@ChartSelect(name = "新增用户职业分布", key = 2)
		@ChartData1(name = "新增用户职业分布", key = 2)
		private int newusercount;

		public SchoolGameChartData() {
		}
		
		// 根据日期排序的比较器
		public final Comparator<SchoolGameChartData> comparatorId = new Comparator<SchoolGameChartData>() {
			public int compare(SchoolGameChartData s1, SchoolGameChartData s2) {
				return new Integer(s2.schoolid).compareTo(new Integer(s1.schoolid));
			}
		};
		
		public SchoolGameChartData(int schoolid) {
            setSchoolid(schoolid);
		}
		
		public int getSchoolid() {
			return schoolid;
		}

		public void setSchoolid(int schoolid) {
			this.schoolid = schoolid;
			this.school = GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(schoolid + "") == null ? schoolid + "" : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(schoolid + "");
		}
		
		public int getActiveusercount() {
			return activeusercount;
		}

		public void setActiveusercount(int activeusercount) {
			this.activeusercount = activeusercount;
		}

		public int getNewusercount() {
			return newusercount;
		}

		public void setNewusercount(int newusercount) {
			this.newusercount = newusercount;
		}

		public String getSchool() {
			return school;
		}

		public void setSchool(String school) {
			this.school = school;
		}
	}
	
	// 详细
	public class SchoolGameData {
		private String dateStr;
		private int activeUser;
		private int newUser;
		private Map<String, Integer> activeUserSchoolCounts;
		private Map<String, Integer> newUserSchoolCounts;
		
		public SchoolGameData() {
			activeUserSchoolCounts = new LinkedHashMap<>();
			newUserSchoolCounts = new LinkedHashMap<>();
			
			for (Entry<String, String> entry : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).entrySet()) {
				if (!GameParamConfigManager.ALL_FLAT.equals(entry.getKey())) {
					activeUserSchoolCounts.put(entry.getKey(), 0);
					newUserSchoolCounts.put(entry.getKey(), 0);
				}
			}
		}
		
		// 根据日期排序的比较器
		public final Comparator<SchoolGameData> comparatorDate = new Comparator<SchoolGameData>() {
			public int compare(SchoolGameData s1, SchoolGameData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};
		
		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public Map<String, Integer> getActiveUserSchoolCounts() {
			return activeUserSchoolCounts;
		}

		public void setActiveUserSchoolCounts(Map<String, Integer> activeUserSchoolCounts) {
			this.activeUserSchoolCounts = activeUserSchoolCounts;
		}

		public Map<String, Integer> getNewUserSchoolCounts() {
			return newUserSchoolCounts;
		}

		public void setNewUserSchoolCounts(Map<String, Integer> newUserSchoolCounts) {
			this.newUserSchoolCounts = newUserSchoolCounts;
		}

		public int getActiveUser() {
			return activeUser;
		}

		public void setActiveUser(int activeUser) {
			this.activeUser = activeUser;
		}

		public int getNewUser() {
			return newUser;
		}

		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}
	}
}
